package com.ifast.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.base.ApiBaseController;
import com.ifast.common.utils.Const;
import com.ifast.integral.domain.IntegralHistory;
import com.ifast.integral.service.IntegralExplainService;
import com.ifast.integral.service.IntegralFromService;
import com.ifast.integral.service.IntegralHistoryService;
import com.ifast.integral.service.IntegralUserService;
import com.ifast.sys.domain.UserDO;
import com.luculent.util.extension.api.Result;
import com.luculent.util.extension.api.PageResult;
import com.luculent.util.extension.api.Result;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;

/**
 * <pre>
 *   积分接口
 * </pre>
 * 
 * <small>2019年4月10日15:28:01 | ShiQinag</small>
 */
@RestController
@RequestMapping("/api/integral/")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ApiIntegralController extends ApiBaseController{
	
	@Autowired
    private IntegralFromService integralFromService;  
	@Autowired
    private IntegralExplainService integralExplainService;  
	@Autowired
    private IntegralHistoryService integralHistoryService;  
	@Autowired
    private IntegralUserService integralUserService;
	  
    @RequestMapping(value = "timeLong.do")
    @ApiOperation( value = "阅读和学习积分时长", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = Result.class)
    
    public Result<?> timeLong(
    		@ApiParam(name = "businessId", required = true, value = "业务id(文章id)") @RequestParam(value = "businessId",required=true) String businessId,
        	@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	try { 
    		UserDO user = getUser(token);
    		return integralFromService.timeLong(user.getId().toString(),user.getDwCode(),businessId);
        } catch (Exception e) { 
        	e.printStackTrace(); 
			return Result.fail_msg("查询异常");
		}
    }
    
    @RequestMapping(value = "addRead.do")
    @ApiOperation( value = "文章阅读积分", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = Result.class)
    
    public Result<?> addRead(
    		@ApiParam(name = "businessId", required = true, value = "业务id(文章id)") @RequestParam(value = "businessId",required=true) String businessId,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	try { 
    		UserDO user = getUser(token);
    		integralFromService.obtainIntegral(user.getId().toString(), user.getDwCode(), Const.integral.integral_read, businessId);
    		return Result.ok();
        } catch (Exception e) { 
        	e.printStackTrace(); 
			return Result.fail_msg("查询异常");
		}
    }
    
    @RequestMapping(value = "addLearn.do")
    @ApiOperation( value = "文章学习积分", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = Result.class)
    
    public Result<?> addLearn(
    		@ApiParam(name = "businessId", required = true, value = "业务id(文章id)") @RequestParam(value = "businessId",required=true) String businessId,
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	try { 
    		UserDO user = getUser(token);
    		integralFromService.obtainIntegral(user.getId().toString(), user.getDwCode(), Const.integral.integral_learn, businessId);
    		return Result.ok();
        } catch (Exception e) { 
        	e.printStackTrace(); 
			return Result.fail_msg("查询异常");
		}
    } 
  
    @RequestMapping(value = "list.do")
    @ApiOperation( value = "积分明细记录", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = PageResult.class)
    
    public PageResult<?> list(
    		@ApiParam(name = "nian", required = false, value = "年") @RequestParam(value = "nian",required=false) String nian,
    		@ApiParam(name = "yue", required = false, value = "月") @RequestParam(value = "yue",required=false) String yue,
    		@ApiParam(name = "ri", required = false, value = "日") @RequestParam(value = "ri",required=false) String ri,
    		@ApiParam(name = "pageNum", required = false, value = "页码从0开始,hasNext判断是否有下一页") @RequestParam(value = "pageNum",required=false) Integer pageNum,
            @ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
        
    	try { 
    		UserDO user = getUser(token);
    		 
         	Page<IntegralHistory> page = new Page<>();
            page.setCurrent((pageNum == null || pageNum == 0) ? 1: pageNum + 1 );
            return this.integralHistoryService.pageList(user.getId().toString(),user.getDwCode(),nian,yue,ri,page);
        } catch (Exception e) {
        	e.printStackTrace();  
			return PageResult.fail_msg("查询异常");
		}
    } 
    
    @RequestMapping(value = "info.do")
    @ApiOperation( value = "个人积分信息", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = Result.class)
    
    public Result<?> info(
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	try {  
    		return integralUserService.info(getUser(token).getId().toString());
        } catch (Exception e) { 
			e.printStackTrace(); 
			return Result.fail_msg("查询异常");
		}
    } 
    
    @RequestMapping(value = "rule.do")
    @ApiOperation( value = "积分规则", httpMethod = "POST",notes="") 
    @ApiResponse(code = 200, message = "success", response = Result.class) 
    public Result<?> rule(
    		@ApiParam(name = "Authorization", required = true, value = "token") @RequestHeader("Authorization") String token){
    	try {  
    		return integralExplainService.rule(Const.integralExplain.rule, getUser(token).getDwCode());
        } catch (Exception e) { 
			e.printStackTrace(); 
			return Result.fail_msg("查询异常");
		}
    } 
    
}
